<template>
  <div>
    <div v-model="searchData" style="margin: 10px 0">
      <el-input v-model="searchData.bookName" style="width: 200px" placeholder="请输入书名"
                suffix-icon="el-icon-search"></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="rest">重置</el-button>

    </div>
    <el-table :data="tableData" border stripe :header-cell-class-name="headerBg">
      <!--      <el-table-column type="selection" width="55"></el-table-column>-->
      <!--      <el-table-column prop="id" label="Id" width="40"></el-table-column>-->
      <el-table-column prop="borrowId" label="借阅号"></el-table-column>
      <el-table-column prop="bookName" label="书名"></el-table-column>
      <el-table-column prop="borrowTime" label="借阅时间"></el-table-column>
      <el-table-column prop="checkTime" label="审核时间"></el-table-column>
      <el-table-column prop="status" label="审核状态">
        <template v-slot="scope">
          <span>{{ scope.row.status === 0 ? "待审核" : scope.row.status === 2 ? "审核不通过" : "审核通过" }} </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="danger" v-if="scope.row.status===0" @click="del(scope.row,3)">取消申请<i class="el-icon-remove-outline"></i></el-button>
          <el-button type="primary" v-if="scope.row.status===1" @click="del(scope.row,2)">归还 <i class="el-icon-remove-outline"></i></el-button>
          <el-button type="primary" v-if="scope.row.status===2" @click="del(scope.row,1)">删除 <i class="el-icon-remove-outline"></i></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="current"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "BorrowCart",
  data() {
    return {
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      multipleSelection: [],
      form: {},
      info: {},
      tableData: [],
      searchOptions: [],
      headerBg: 'headerBg',
      current: 0,
      total: 0,
      pageSize: 5,
      dialogFormVisible: false,
      updateFormVisible: false,
      options: [],
      msg: '',
      searchData: {
        bookName: '',
        nickName: '',
        name: ''
      }
    }
  },
  created() {
    this.load()
    this.getSearchOption()
  },
  methods: {
    getSearchOption() {
      this.request.get("/borrow/getUserOptions").then(res => {
        this.searchOptions = res.data
      })
    },
    changeUser() {
      this.load()
    },
    rest() { //重置搜索栏
      this.searchData.nickName = ''
      this.searchData.bookName = ''
      this.load()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
      console.log(this.multipleSelection.map(v => v.id))
    },

    del(row, type) {
      if (type === 3) {
        this.$confirm('确定取消申请吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.request.delete("/borrow/delete/" + row.bookId+"/"+type).then(resp => {
            if (resp.code === 200) {
              this.load()
              this.$message({
                type: 'success',
                message: '取消申请成功!',
              });
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          });
        });
      }
      if (type === 2) {
        this.$confirm('确定归还吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.request.delete("/borrow/delete/" + row.bookId+"/"+type).then(resp => {
            if (resp.code === 200) {
              this.load()
              this.$message({
                type: 'success',
                message: '归还成功!',
              });
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消归还'
          });
        });
      }

    },
    addHander() {
      this.dialogFormVisible = true
      this.form = {}
    },
    handleSizeChange(pageSize) {
      console.log("每页条数: " + pageSize)
      this.pageSize = pageSize;
      this.load()
    },
    handleCurrentChange(current) {
      console.log("当前页码: " + current)
      this.current = current;
      this.load()
    },
    collapse() {  // 点击收缩按钮触发
      this.isCollapse = !this.isCollapse
      if (this.isCollapse) {  // 收缩
        this.sideWidth = 64
        this.collapseBtnClass = 'el-icon-s-unfold'
        this.logoTextShow = false
      } else {   // 展开
        this.sideWidth = 200
        this.collapseBtnClass = 'el-icon-s-fold'
        this.logoTextShow = true
      }
    },
    load() {
      this.request.get("/borrow/page", {
            params: {
              current: this.current,
              pageSize: this.pageSize,
              bookName: this.searchData.bookName,
              nickName: this.searchData.nickName
            }
          }
      ).then(resp => {
        console.log(resp)
        this.tableData = resp.data.records
        this.total = resp.data.total
      })
    },
  }
}
</script>

<style>
.headerBg {
  background: #eee !important;
}
</style>